package com.raja.servingStation.controller.notice;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.common.constant.MysqlPageVo;
import com.raja.servingStation.common.exception.BusinessRuntimeException;
import com.raja.servingStation.common.util.RequestUtil;
import com.raja.servingStation.common.util.ResponseUtil;
import com.raja.servingStation.entity.ServingStation;
import com.raja.servingStation.entity.vo.NoticeReadingDetail;
import com.raja.servingStation.service.NoticeService;
import com.raja.servingStation.service.ServingStationService;
import com.raja.servingStation.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 公文相关
 */
@Controller
public class NoticeController {
    private static final Logger log = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private ResponseUtil responseUtil;

    @Autowired
    NoticeService noticeService;

    @Autowired
    ServingStationService servingStationService;

//    @RequestMapping(value = "/system/user")
//    public void getUser(HttpServletResponse response){
//        try {
//            testService.select();
//            response.getWriter().write("55555555555555");
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }

//    @RequestMapping("/send")
//    public void send(HttpServletResponse response){
//
//        try {
//            response.getWriter().write("ok");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//    @RequestMapping(value = "insert")
//    public void weilcome(HttpServletRequest request ,HttpServletResponse response){
//        try {
//            testService.save();
//        }catch (Exception e){
//            log.error(e.getMessage(),e);
//        }
//    }



    /**
     * 公文列表接口
     * @param request
     * @return
     */
    @RequestMapping(value = "/notice/list", method = RequestMethod.GET)
    public ResponseEntity orderListNew(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
//            checkData(body);
            MysqlPageVo pageVO = noticeService.queryOrderListByPage(body);
            return responseUtil.success(pageVO);
        } catch (BusinessRuntimeException bb) {
            log.error(bb.getMessage(), bb);
            return responseUtil.exception(bb.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("查询列表异常");
        }
    }


    // 发布公文接口
    @RequestMapping(value = "/notice/add", method = {RequestMethod.GET,RequestMethod.POST})
    public ResponseEntity addNotice(HttpServletRequest request) throws Exception {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            noticeService.addNotice(body);
            return responseUtil.success("公文发布成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return responseUtil.exception("公文发布异常");
        }
    }


    // 发公文 待选择的接收方 回显到combo
    @RequestMapping(value = "/station/list", method = RequestMethod.GET)
    public ResponseEntity getStationList(HttpServletRequest request) {
        try {
            List<ServingStation> servingStations = servingStationService.queryServingStations();
            return responseUtil.success(servingStations);
        } catch (BusinessRuntimeException bb) {
            log.error(bb.getMessage(), bb);
            return responseUtil.exception(bb.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("查询列表异常");
        }
    }


    // 公告列表：公告阅读明细
    @RequestMapping(value = "/noticeReadingDetail/list", method = RequestMethod.GET)
    public ResponseEntity getNoticeReadingDetailList(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            List<NoticeReadingDetail> noticeReadingDetails = noticeService.queryNoticeReadingDetail(body);
            return responseUtil.success(noticeReadingDetails);
        } catch (BusinessRuntimeException bb) {
            log.error(bb.getMessage(), bb);
            return responseUtil.exception(bb.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("查询列表异常");
        }
    }


    // 补发布公文接口
    @RequestMapping(value = "/notice/send2station", method = {RequestMethod.GET,RequestMethod.POST})
    public ResponseEntity sendNotice2Station(HttpServletRequest request) throws Exception {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            noticeService.sendNotice2Station(body);
            return responseUtil.success("公文补发布成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return responseUtil.exception("公文补发布异常");
        }
    }


    // 公文撤销接口
    @RequestMapping(value = "/notice/cancel", method = {RequestMethod.GET,RequestMethod.POST})
    public ResponseEntity cancelNotice(HttpServletRequest request) throws Exception {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            noticeService.cancelNotice(body);
            return responseUtil.success("撤销公文成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return responseUtil.exception("撤销公文异常");
        }
    }

}
